<?php

namespace App\Http\Controllers;

use App\Jobs\SendEmail;
use App\Student;
use http\Env\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;

class StudentController extends Controller
{

    public function orm1()
    {
        //$students = Student::all();//查询Student模型的所有记录
        //$students = Student::find(1);//按主键查询
        //$students = Student::findOrFail(1);//按主键查询,失败则抛出异常
        $students = Student::get();
        dd($students);//打印

    }
    public function orm2()
    {   //使用模型新增数据
        /*$student = new Student();
        $student->name = 'name2';
        $student->id = 546;
        $student->save();*/

        //使用模型的create方法新增数据
      /*  $student = Student::create(
            ['name' => 'imooc', 'id' => 888]
        );*/

      //使用firstOrCreate()查找数据，没有则新增
       /* $student = Student::firstOrCreate(
            ['name' => 'hahah', 'id' => 43543]
        );*/
    }

    public function orm3()
    {
        //通过模型更新数据
/*        $student = Student::find(1);
        $student->name = 'student';
        $student->save();*/
        //结合查询语句批量更新
        Student::where('id','>',1)->update(
            ['name' => 'update']
        );
    }

    public function orm4()
    {
        //通过模型删除
        //$student = Student::find(3)->delete();
        //通过主键删除
        Student::destroy(2);
    }

    public function request1(Request $request)
    {

    }

    public function session1(Request $request)
    {
        //1. HTTP request session()
  /*      $request->session()->put('key1','value1');//写入
        echo $request->session()->get('key1');//取出
        */

        //2.session()
        //session()->put('key2','value2');
       //echo session()->get('key2');

        //3.Session
        //Session::put('key3','value3');


    }
    public function session2(Request $request)
    {
        return Session::get('message3');
    }


    public function response()
    {
        //响应json
/*        $data = [
            'errCode' => 0,
            'errMsg' => 'success',
            'data' => 'test'
        ];
        return response()->json($data);*/

        //重定向
        //return redirect('session2')->with('message3','成功');
        return redirect()->route('session2')->with('message3','成功');

    }

    public function activity0()
    {
        return '活动未开始';
    }
    public function activity1()
    {
        return '活动进行中';
    }
    public function activity2()
    {
        return '活动结束';
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $students = Student::paginate(6);
        return view('student.index',compact('students'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $student = new Student();
        return view('student.create',compact('student'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    public function store(Request $request)
    {
        $data=$request->input('Student');

        $student = new Student();
        $student->name = $data['name'];
        $student->sex = $data['sex'];
        $student->age = $data['age'];
        $student->email = $data['email'];
//控制器验证
/*        $this->validate($request,[
           'Student.name'=>'required|min:2|max:20',
            'Student.age'=>'required|integer',
            'Student.sex'=>'required|integer',
            'Student.email'=>'required|email'
        ],[
            'required' => ':attribute 为必填项',
            'min'=>':attribute 长度不符合',
            'integer'=>':attribute 必须为整数'
        ],[
            'Student.name'=>'姓名',
            'Student.age'=>'年龄',
            'Student.sex'=>'性别',
            'Student.email'=>'邮箱'
        ]);*/
    //Validator类验证
        $validator = \Validator::make($request->input(),[
            'Student.name'=>'required|min:2|max:20',
            'Student.age'=>'required|integer',
            'Student.sex'=>'required|integer',
            'Student.email'=>'required|email'
        ],[
            'required' => ':attribute 为必填项',
            'min'=>':attribute 长度不符合',
            'integer'=>':attribute 必须为整数'
        ],[
            'Student.name'=>'姓名',
            'Student.age'=>'年龄',
            'Student.sex'=>'性别',
            'Student.email'=>'邮箱'
        ]);

        if($validator->fails()){
            return redirect()->back()->witherrors($validator)->withInput();
        }

        if($student->save()){
            return redirect()->route('student.index')->with('success','数据操作成功');
        }else{
            return redirect()->back();
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function show(Student $student)
    {
        return view('student.show',compact('student'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function edit(Student $student)
    {
        return view('student.update',compact('student'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Student $student)
    {
        $data = $request->input('Student');
        $student->name = $data['name'];
        $student->age = $data['age'];
        $student->sex = $data['sex'];

        $validator = \Validator::make($request->input(),[
            'Student.name'=>'required|min:2|max:20',
            'Student.age'=>'required|integer',
            'Student.sex'=>'required|integer',
            'Student.email'=>'required|email'
        ],[
            'required' => ':attribute 为必填项',
            'min'=>':attribute 长度不符合',
            'integer'=>':attribute 必须为整数'
        ],[
            'Student.name'=>'姓名',
            'Student.age'=>'年龄',
            'Student.sex'=>'性别',
            'Student.email'=>'邮箱'
        ]);

        if($validator->fails()){
            return redirect()->back()->witherrors($validator)->withInput();
        }
        if($student->save()){
            return redirect('student')->with('success','修改成功');
        }else{
            return redirect()->back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Student  $student
     * @return \Illuminate\Http\Response
     */
    public function destroy(Student $student)
    {
        //
    }
    public function delete(Student $student)
    {
        if($student->delete()){
            return redirect('student')->with('success','删除成功');
        }else{
            return redirect()->back();
        }
    }

    public function upload(Request $request)
    {
        if($request->isMethod('POST')){
            //var_dump($_FILES);
            $file=$request->file('file');
            //文件是否上传成功
            if($file->isValid()){
                $originalName = $file->getClientOriginalName();//源文件名
                $ext = $file->getClientOriginalExtension();//扩展名
                $type = $file->getClientMimeType();//文件类型
                $realPath = $file->getRealPath();//临时绝对路径

                $filename = date('Y-m-d').'-'.uniqid().'-'.$originalName.'.'.$ext;

                $bool=Storage::disk('uploads')->put($filename,file_get_contents($realPath));//存入磁盘

                if($bool!=null){
                    return redirect()->back()->with('success','文件上传成功');
                }
            } else{
                return redirect()->back()->with('error','文件上传失败');
            }
        }
        return view('student.upload');
    }
    public function mail()
    {
/*        Mail::raw('邮件内容',function($message){
            $message->from('json_vip@163.com','邮件测试');
            $message->subject('邮件主题 测试');
            $message->to('834416351@qq.com');
        });*/
        Mail::send('welcome',['student'=>'1'],function($message){
            $message->to('151@qq.com');
        });
    }
    public function cache1()
    {
        //Cache::put('key1','val1',10);
        //Cache::add('key1','val1',10);
        Cache::forever('key2','val2');
    }
    public function cache2()
    {
        //get
        //Cache::get('key1');
        //Log::info('test');
        //dispatch(new SendEmail('834416351@qq.com'));
        return 1;
    }


}
